package emulator.shell.GUI.debugger;

import java.util.LinkedList;
import java.util.Observer;

import emulator.assembler.CodeAnalyzer;
import emulator.assembler.DataDescriptionTable;

public class CodePanelAnalyzer {
	private DataDescriptionTable description_table;
	private boolean symbols_analyzed;
	private AnalyzerThread thread;

	public CodePanelAnalyzer()
	{
		description_table = null;
		thread = null;
		symbols_analyzed = false;
	}

	public void startAnalysis(int address, CodeInfo info, Observer observer)
	{
		LinkedList<Integer> addresses = new LinkedList<Integer>();
		if (!isSymbolsAnalyzed())
		{
			addresses = info.getAssembler().getSymbols().getCodeLabels();
			setSymbolsAnalyzed(true);
		}
		addresses.push(address);

		thread = new AnalyzerThread();
		description_table = new DataDescriptionTable(info.getDescriptionTable());
		thread.setAnalyzer(
				new CodeAnalyzer(info.getAssembler(), description_table, info.getMemory(), addresses)
				);
		thread.addObserver(observer);
		thread.start();
	}
	
	public DataDescriptionTable getDescriptionTable() {
		return description_table;
	}

	public void setDescriptionTable(DataDescriptionTable description_table) {
		this.description_table = description_table;
	}

	public boolean isSymbolsAnalyzed() {
		return symbols_analyzed;
	}

	public void setSymbolsAnalyzed(boolean symbols_analyzed) {
		this.symbols_analyzed = symbols_analyzed;
	}

	public boolean isRunning() {
		return thread != null;
	}

	public void ackStop() {
		thread = null;
	}
}